<template>
  <a-modal
    :title="title"
    :width="containerWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('systemManagement.close')"
    wrapClassName="ant-modal-cust-warp"
    style="top:20%;height: 60%;overflow-y: hidden;transition-property: width; transition-duration: 100ms"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="24">
          <a-col :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('popup.settlementAccount1')">
              <a-select :placeholder="$t('setailManagement.pleaseAccount')" v-decorator="[ 'oneAccountId' ]" :dropdownMatchSelectWidth="false" allowClear @select="onSelect($event, 1)">
                <a-select-option v-for="(item,index) in accountList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('popup.settlementAmount')">
              <a-input-number :placeholder="$t('popup.pleaseEnterTheAmount')" v-decorator.trim="[ 'oneAccountPrice' ]" />
            </a-form-item>
          </a-col>
          <a-col v-if="showCheckNumber1" :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('financialManagement.checkNumber')">
              <a-input v-decorator.trim="[ 'checkNumber1' ]" :placeholder="$t('financialManagement.pleaseEnterCheckNumber')" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('popup.settlementAccount2')">
              <a-select :placeholder="$t('setailManagement.pleaseAccount')" v-decorator="[ 'twoAccountId' ]" :dropdownMatchSelectWidth="false" allowClear @select="onSelect($event, 2)">
                <a-select-option v-for="(item,index) in accountList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('popup.settlementAmount')">
              <a-input-number :placeholder="$t('popup.pleaseEnterTheAmount')" v-decorator.trim="[ 'twoAccountPrice' ]" />
            </a-form-item>
          </a-col>
          <a-col v-if="showCheckNumber2" :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('financialManagement.checkNumber')">
              <a-input v-decorator.trim="[ 'checkNumber2' ]" :placeholder="$t('financialManagement.pleaseEnterCheckNumber')" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('popup.settlementAccount3')">
              <a-select :placeholder="$t('setailManagement.pleaseAccount')" v-decorator="[ 'threeAccountId' ]" :dropdownMatchSelectWidth="false" allowClear @select="onSelect($event, 3)">
                <a-select-option v-for="(item,index) in accountList" :key="index" :value="item.id">
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('popup.settlementAmount')">
              <a-input-number :placeholder="$t('popup.pleaseEnterTheAmount')" v-decorator.trim="[ 'threeAccountPrice' ]" />
            </a-form-item>
          </a-col>
          <a-col v-if="showCheckNumber3" :lg="colWidth" :md="colWidth" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('financialManagement.checkNumber')">
              <a-input v-decorator.trim="[ 'checkNumber3' ]" :placeholder="$t('financialManagement.pleaseEnterCheckNumber')" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import pick from 'lodash.pick'
  import {getAccount} from '@/api/api'
  export default {
    name: 'ManyAccountModal',
    data () {
      return {
        title:this.$t('setailManagement.operation'),
        visible: false,
        model: {},
        accountList: [],
        accountIdList: [],
        accountMoneyList: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 },
          lg: { span: 8 },
          md: { span: 8 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
          lg: { span: 16 },
          md: { span: 16 }
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        showCheckNumber1: false,
        showCheckNumber2: false,
        showCheckNumber3: false,
        containerWidth: 650,
        colWidth: 12
      }
    },
    created () {
    },
    // watch: {
    //   visible (val) {
    //     if (!val) {
    //       this.showCheckNumber1 = this.showCheckNumber2 = this.showCheckNumber3 = false
    //       this.setContainerWidth(false)
    //     }
    //   }
    // },
    methods: {
      edit (idStr, moneyStr) {
        this.initAccount()
        this.form.resetFields();
        this.model = Object.assign({}, {});
        let idList = [], moneyList = []
        if(idStr && idStr.indexOf(',')>-1) {
          idList = idStr.split(",")
          moneyList = moneyStr.split(",")
        } else {
          idList = idStr
          moneyList = moneyStr
        }
        if(idList[0]) {this.model.oneAccountId = idList[0]}
        if(idList[1]) {this.model.twoAccountId = idList[1]}
        if(idList[2]) {this.model.threeAccountId = idList[2]}
        if(moneyList[0]) {this.model.oneAccountPrice = Math.abs(moneyList[0])}
        if(moneyList[1]) {this.model.twoAccountPrice = Math.abs(moneyList[1])}
        if(moneyList[2]) {this.model.threeAccountPrice = Math.abs(moneyList[2])}
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'oneAccountId','oneAccountPrice',
            'twoAccountId','twoAccountPrice','threeAccountId','threeAccountPrice'))
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            let allPrice = 0
            that.confirmLoading = true;
            that.accountIdList = []
            that.accountMoneyList = []
            let formData = Object.assign(this.model, values);
            if(formData.oneAccountId!==undefined) {
              that.accountIdList.push(formData.oneAccountId)
            }
            if(formData.twoAccountId!==undefined) {
              that.accountIdList.push(formData.twoAccountId)
            }
            if(formData.threeAccountId!==undefined) {
              that.accountIdList.push(formData.threeAccountId)
            }
            if(formData.oneAccountPrice!==undefined) {
              that.accountMoneyList.push(formData.oneAccountPrice)
              allPrice = allPrice + formData.oneAccountPrice
            }
            if(formData.twoAccountPrice!==undefined) {
              that.accountMoneyList.push(formData.twoAccountPrice)
              allPrice = allPrice + formData.twoAccountPrice
            }
            if(formData.threeAccountPrice!==undefined) {
              that.accountMoneyList.push(formData.threeAccountPrice)
              allPrice = allPrice + formData.threeAccountPrice
            }
            if(that.accountIdList.length<2 || that.accountMoneyList.length<2) {
              this.$message.warning(this.$t('popup.sorryMoreThanTwo'));
              that.confirmLoading = false;
              return;
            }
            if((formData.oneAccountId && !formData.oneAccountPrice)||
              (formData.twoAccountId && !formData.twoAccountPrice)||
              (formData.threeAccountId && !formData.threeAccountPrice)) {
              this.$message.warning(this.$t('popup.sorryPleaseFillAmount'));
              that.confirmLoading = false;
              return;
            }
            let checkList = [formData.checkNumber1 || '',formData.checkNumber2 || '',formData.checkNumber3 || '']
            that.$emit('ok', that.accountIdList, that.accountMoneyList, allPrice,checkList);
            that.confirmLoading = false;
          }
        })
      },
      handleCancel () {
        this.close()
      },
      initAccount(){
        let that = this;
        getAccount({}).then((res)=>{
          if(res && res.code === 200) {
            that.accountList = res.data.accountList
          }
        })
      },
      onSelect (value, index) {
        const item = this.accountList.find(item => item.id === value)
        const IS_CHECK_PAY = '1'
        const showCheckNumberArray = ['showCheckNumber1', 'showCheckNumber2', 'showCheckNumber3']
        if (item.type && item.type.toString() === IS_CHECK_PAY) {
          this[`showCheckNumber${index}`] = true
        } else {
          this[`showCheckNumber${index}`] = false
        }
        this.setContainerWidth(showCheckNumberArray.some(item => this[item]))
      },
      setContainerWidth (condition) {
        if (condition) {
          this.containerWidth = 800
          this.colWidth = 8
        } else {
          this.containerWidth = 650
          this.colWidth = 12
        }
      }
    }
  }
</script>

<style scoped lang="less">
  ::v-deep .ant-input-number {
    width: 100%;
  }
</style>